package com.bt.service.impl;

import com.bt.dto.UserEditArticleDTO;
import com.bt.dto.UserUpdateDTO;
import com.bt.entity.YddArticle;
import com.bt.entity.YddUser;
import com.bt.mapper.UserMapper;
import com.bt.result.PageResult;
import com.bt.result.list;
import com.bt.service.UserService;
import com.bt.vo.UserDateList2VO;
import com.bt.vo.UserDateListVO;
import com.bt.vo.UserMyArticlePageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author XJL
 * @Date 2023-05-04 15:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 我的文章
     *
     * @param limit
     * @param page
     * @return
     */
    @Override
    public PageResult page(Integer limit, Integer page) {
        PageHelper.startPage(page,limit);
        Page<UserMyArticlePageVO> userMyArticlePageVOS = userMapper.page();
        return new PageResult(userMyArticlePageVOS.getTotal(),userMyArticlePageVOS.getPages(),
                userMyArticlePageVOS.getPageSize(),userMyArticlePageVOS.getPageNum(),userMyArticlePageVOS.getResult());
    }


    /**
     * 我的收藏/点赞文章
     *
     * @param page
     * @param limit
     * @param type
     * @return
     */
    @Override
    public PageResult userDataList(Integer page, Integer limit, Integer type) {
        PageHelper.startPage(page,limit);
        Page<UserDateList2VO> userDateListVOPage = userMapper.userDataList(type);
        return new PageResult(userDateListVOPage.getTotal(), userDateListVOPage.getPages(), userDateListVOPage.getPageSize()
                , userDateListVOPage.getPageNum(), userDateListVOPage.getResult());
    }


    /**
     * 修改文章
     *
     * @param userEditArticleDTO
     */
    @Override
    public void update(UserEditArticleDTO userEditArticleDTO) {
        YddArticle yddArticle = new YddArticle();
        BeanUtils.copyProperties(userEditArticleDTO,yddArticle);
        //补全数据
        long time = System.currentTimeMillis();
        yddArticle.setUpdate_date(time);

        userMapper.update(yddArticle);
    }


    /**
     * 修改用户资料
     *
     * @param userUpdateDTO
     */
    @Override
    public void updateUser(UserUpdateDTO userUpdateDTO) {
        YddUser yddUser = new YddUser();
        BeanUtils.copyProperties(userUpdateDTO,yddUser);
        userMapper.updateUser(yddUser);
    }

}
